<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Setting extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
		$this->load->library('tank_auth');
		$this->lang->load('tank_auth');
        if(!$this->tank_auth->is_logged_in())
            redirect('administrator/auth');
        $this->load->model('languages','lan');
        $this->load->model('articles','art');
        $this->load->model('categories','categories');
        $this->load->model('products','pro');
        
        
    }

    /**
     * Default function
     */
    public function index(){
        redirect('administrator/setting/general');
    }
    public function language($act = false){
        
            $post= $this->input->post(null,true);
            $this->_layout['action'] = $this->uri->segment(4);
            $this->_layout['lang_id'] = $this->uri->segment(5);
            
        $this->assign('action','');
        $languages = $this->lan->get_language();
        $this->assign('language',$languages);
        if($act)
        {
            
            if($act=="save"){
                if($post['name'] =="" || $post['code']=="" ){
                            _set_error_message('....');
                            redirect('administrator/setting/language');
                        }
                if($post['isdefault']){
                    $this->session->set_userdata('lan',$post['code']);
                }
               
                switch ($post['action']) {
                    
                    case 'create':                            
                       $rs = $this->lan->add_language(array('lang_code'=>$post['code']
                                                        ,'lang_name'=>$post['name']
                                                        ,'actived'=>$post['actived']
                                                        ,'is_default'=>$post['isdefault']));
                       if($rs>0)
                            _set_success_message('Save successful');
                       else
                            _set_error_message('....');
                       redirect('administrator/setting/language');
                        break;
                    case 'edit':
                        $rs = $this->lan->update_language(array('lang_code'=>$post['code']
                                                        ,'lang_name'=>$post['name']
                                                        ,'actived'=>$post['actived']
                                                        ,'is_default'=>$post['isdefault']),$post['lang_id']);
                        if($rs>0)
                            _set_success_message('Save successful');
                       else
                            _set_error_message('....');
                       redirect('administrator/setting/language');
                        break;
                }
                
            }elseif($act=="delete"){
                $rs = $this->lan->delete_language($this->_layout['lang_id']);
                if($rs>0)
                            _set_success_message('Save successful');
                redirect('administrator/setting/language');
            }elseif($act=="edit"){
               $this->_layout['language'] = $this->lan->get_language($this->_layout['lang_id']);
               
            }
            $form   = $this->load->view('layouts/form/language',$this->_layout,true);
            $this->assign('action',$form);
            
        }
    }
    
    
    public function general($act = false){
        $post =$this->input->post(null);
        if(!$act){
            
        }else
        {
            if($act == 'save'){
                $this->load->helper('file');
                if(!isset($_POST['register']))
                   
                   $post['register'] = "0";
                 
                   
                $data ='<?php '."\n";
                
                foreach($post as $key=>$val){
                    $data.='$config["'.$key.'"]="'.htmlspecialchars($val).'";'."\n";
                }
                
                if(!write_file('application/inc/config.php',$data))
                    _set_error_message('...');
                else
                    _set_success_message('Settings saved.');
                redirect('administrator/setting/general');
            }
        }
    }
    public function country($act = false){
        if($act=='save'){
            $this->db->insert('countries',array('name'=>$this->input->post('name'),'parent_id'=>$this->input->post('parent_id')));
            redirect('administrator/setting/country');
        }
        if($act=='delete'){
            $this->db->where('id',$this->uri->segment(5));
            $this->db->delete('countries');redirect('administrator/setting/country');
        }
    }
    public function translate($act = false){
        $post = $this->input->post(null,true);
        $this->_layout['action'] = $this->uri->segment(4);
        if(!$act)   redirect('administrator/setting/translate/post');
        $form="";
        $id = $this->uri->segment(5);
        switch ($act){
            case "save":
                $this->disable_view();
                 switch ($post['action']) {
                    case "post":
                        $article = $this->art->get_article($post['post_id']);
                        $posttitle = $this->_global->_unserialize($article->post_title);
                        $postdesc = $this->_global->_unserialize($article->post_desc);
                        $postcontent = $this->_global->_unserialize($article->post_content);
                        $seotitle = $this->_global->_unserialize($article->seo_title);
                        $seoalias = $this->_global->_unserialize($article->seo_alias);
                        
                        if(!empty($post['lang'])){
                           $posttitle[$post['lang']]['post_title'] = $post['title'];
                           $postdesc[$post['lang']]['post_desc'] = $post['post_desc'];
                           $postcontent[$post['lang']]['post_content'] = $post['content'];
                           $seotitle[$post['lang']]['seo_title'] = $post['seo_title'];
                           $seoalias[$post['lang']]['alias'] = _set_alias($post['seo_title']);
                           
                           $rs = $this->art->update_article(array('post_title'=>$this->_global->_serialize($posttitle)
                                                                ,'post_desc'=>$this->_global->_serialize($postdesc)
                                                                ,'post_content'=>$this->_global->_serialize($postcontent)
                                                                ,'seo_title'=>$this->_global->_serialize($seotitle)
                                                                ,'seo_alias'=>$this->_global->_serialize($seoalias)
                                                                ),array('post_id'=>$post['post_id']));                        
                        
                            if($rs>0)
                                _set_success_message('Save successful');
                            else
                                _set_error_message('....');
                            redirect('administrator/setting/translate/post/');
                        }
                    break;
                    case "category":
                        $category = $this->categories->getCat(array('cat_id'=>$post['cat_id']));
                        if(empty($category)) show_404();
                        $catname = $this->_global->_unserialize($category->cat_name);
                        $catalias = $this->_global->_unserialize($category->cat_alias);
                        if(!empty($post['lang'])){
                            $catname[$post['lang']]['title'] = $post['cat_name'];
                            $catalias[$post['lang']]['alias'] = $post['cat_name'];
                            $desc[$post['lang']]['cat_desc'] = $post['cat_desc'];
                            $rs = $this->categories->update_category(array('cat_name'=>$this->_global->_serialize($catname)
                                                                ,'cat_alias'=>$this->_global->_serialize($catalias)
                                                                ,'cat_desc' =>$this->_global->_serialize($desc)
                                                                ),array('cat_id'=>$post['cat_id']));  
                            if($rs>0)
                            _set_success_message('Save successful');
                            else
                                _set_error_message('....');
                            redirect('administrator/setting/translate/category/');    
                        }
                    break;
                    case "product":
                        $product = $this->pro->getProduct(array('product_id'=>$post['product_id']));
                        if(empty($product)) show_404();
                        $productname = $this->_global->_unserialize($product->product_name);
                        $productdesc = $this->_global->_unserialize($product->product_desc);
                        $productalias = $this->_global->_unserialize($product->product_alias);
                        if(!empty($post['lang'])){
                            $productname[$post['lang']]['product_name'] = $post['product_name'];
                            $productdesc[$post['lang']]['product_desc'] = $post['product_desc'];
                            $productalias[$post['lang']]['alias'] = _set_alias($post['product_name']);
                            $rs = $this->pro->updateProduct(array('product_name'=>$this->_global->_serialize($productname)
                                                                ,'product_alias'=>$this->_global->_serialize($productalias)
                                                                ,'product_desc'=>$this->_global->_serialize($productdesc)
                                                                ),array('product_id'=>$post['product_id']));  
                            if($rs>0)
                            _set_success_message('Save successful');
                            else
                                _set_error_message('....');
                            redirect('administrator/setting/translate/product/');    
                        }
                    break;
                 };
            break;
            case "post":
                $this->load->model('articles');
                $this->_layout['data'] = $this->articles->get_article();
                $this->_layout['languages'] = $this->lan->get_language_default(0,1,1);
                $this->_layout['lang'] = $this->uri->segment(6);
                $this->_layout['lan_default'] = $this->lan->get_languages(array('lang_code'=>$this->session->userdata['lan']));
                $this->_layout['lan_to_translate'] = $this->lan->get_languages(array('lang_code'=>$this->uri->segment(6)));
                if($id){
                    if(!is_numeric($id)) show_404();
                    $article = $this->articles->get_article($id);
                    if(empty($article)) show_404();
                    if(isset($_POST['lan']))
                        redirect('administrator/setting/translate/post/'.$id.'/'.$_POST['lan']);
                    $this->_layout['article'] = $article;
                    $form = $this->load->view('layouts/form/edit_article_translate',$this->_layout,true);
                }
                else
                $form   = $this->load->view('layouts/form/article_translate',$this->_layout,true);
                 $this->assign('form',$form);
            break;
            case "category":
                $this->load->model('categories');
                $this->_layout['data'] = $this->categories->getCat();
                $this->_layout['languages'] = $this->lan->get_language_default(0,1,1);
                $this->_layout['lang'] = $this->uri->segment(6);
                $this->_layout['lan_default'] = $this->lan->get_languages(array('lang_code'=>$this->session->userdata['lan']));
                $this->_layout['lan_to_translate'] = $this->lan->get_languages(array('lang_code'=>$this->uri->segment(6)));
                if($id){
                    if(!is_numeric($id)) show_404();
                    $category = $this->categories->getCat(array('cat_id'=>$id));
                    if(empty($category)) show_404();
                    if(isset($_POST['lan']))
                        redirect('administrator/setting/translate/category/'.$id.'/'.$_POST['lan']);
                    $this->_layout['category'] = $category;
                    $form = $this->load->view('layouts/form/edit_category_translate',$this->_layout,true);
                }
                else
                $form   = $this->load->view('layouts/form/category_translate',$this->_layout,true);
                 $this->assign('form',$form);
            break;
            case "product":
                $this->_layout['data'] = $this->pro->getProduct();
                $this->_layout['languages'] = $this->lan->get_language_default(0,1,1);
                $this->_layout['lang'] = $this->uri->segment(6);
                $this->_layout['lan_default'] = $this->lan->get_languages(array('lang_code'=>$this->session->userdata['lan']));
                $this->_layout['lan_to_translate'] = $this->lan->get_languages(array('lang_code'=>$this->uri->segment(6)));
                if($id){
                    if(!is_numeric($id)) show_404();
                    $product = $this->pro->getProduct(array('product_id'=>$id));
                    if(empty($product)) show_404();
                    if(isset($_POST['lan']))
                        redirect('administrator/setting/translate/product/'.$id.'/'.$_POST['lan']);
                    $this->_layout['product'] = $product;
                    $form = $this->load->view('layouts/form/edit_product_translate',$this->_layout,true);
                }
                else
                $form   = $this->load->view('layouts/form/product_translate',$this->_layout,true);
                 $this->assign('form',$form);
            break;
            
        }
    }
    
}